package com.imti.controller;
import com.imti.mapper.PaperMapper;
import com.imti.model.Paper;
import com.imti.model.Repo;
import com.imti.model.dto.PaperTitle;
import com.imti.service.PaperService;
import com.imti.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/papers")
public class PaperController {

    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private PaperService paperService;

    @RequestMapping
    public String getPaperList(PageBean page, Paper paper, HttpServletRequest request) {
        request.setAttribute("page", paperService.findAll(page,paper));
        return "paper/paper-list";
    }

    @RequestMapping("/addPaper")
    public String addPaper(@ModelAttribute("paper") Paper paper,
                           @RequestParam("repoList") List<Integer> repoList) {
        paperService.addPaper(paper);
        int paperId = paperService.selectPaperId(paper.getTitle());
        for (Integer repo : repoList) {
            paperService.saveRepo(paperId,repo);
        }
        return "redirect:/papers";
    }

    @RequestMapping("/showRepo")
    public String showRepo(Model model) {
        List<Repo> repoList = paperService.selectRepo();
        model.addAttribute("repoList",repoList);
        return "paper/add-paper";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
        paperService.deleteRepo(id);
        return "redirect:/papers";
    }

    @RequestMapping("/view/{id}")
    public String view(@PathVariable("id") Integer id,HttpServletRequest request){
        paperService.showPaper(id,request);
        return "paper/paperView";
    }
}
